package cpcn.dsp.demo.tx.corpquery;

import cpcn.dsp.demo.consts.DSPConsts;
import cpcn.dsp.demo.util.HttpsUtil;
import cpcn.dsp.institution.api.security.EncryptAndDecrypt;
import cpcn.dsp.institution.api.system.DSPInstitutionEnvironment;
import cpcn.dsp.institution.api.tx.personalinfo.Tx2230Request;
import cpcn.dsp.institution.api.tx.personalinfo.Tx2230Response;
import cpcn.dsp.institution.api.util.Base64;
import cpcn.dsp.institution.api.util.GUIDGenerator;

import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * Modify Information:
 * Author       Date          Description
 * ============ ============= ============================
 * chenqi   2021年09月18日 Create this file
 * </pre>
 */
public class Test2230 {
    public static void main(String[] args) throws Exception {

        //1.初始化
        DSPInstitutionEnvironment.initialize(DSPConsts.Keystore_test, DSPConsts.myKeystorePassword_test, DSPConsts.publicKey_test, DSPConsts.MSG_ENCRYPT_20);
        //1.1 encryptSN、signSN值不为空，说明初始化证书成功
        System.out.println(DSPInstitutionEnvironment.encryptSN);
        System.out.println(DSPInstitutionEnvironment.signSN);

        //2.生成请求明文
        Tx2230Request tx2230Request = new Tx2230Request();
        tx2230Request.setInstitutionID("");// 机构号由中金分配
        tx2230Request.setTxSN(GUIDGenerator.genGUID());
        tx2230Request.setPhoneNumber("123456789101");
        tx2230Request.setSmsContentCode("120");
        tx2230Request.setRemark("");//仅测试环境判断该字段1->不一致,2->未知，3->请求失败，其他->一致

        //报文处理
        tx2230Request.process();
        String plainText = tx2230Request.getRequestPlainText();
        System.out.println("请求明文：" + plainText);

        String reqMessage = tx2230Request.getRequestMessage();
        String reqSignature = tx2230Request.getRequestSignature();
        String requestDgtlEnvlp = tx2230Request.getRequestDgtlEnvlp();
        String requestSignSN = tx2230Request.getRequestSignSN();
        String requestEncryptSN = tx2230Request.getRequestEncryptSN();
        //生成参数
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("message", reqMessage);
        paramMap.put("signature", reqSignature);
        paramMap.put("dgtlenvlp", requestDgtlEnvlp);
        paramMap.put("signSN", requestSignSN);
        paramMap.put("encryptSN", requestEncryptSN);
        //生成请求参数
        String requestString = EncryptAndDecrypt.createLinkStringByGet(paramMap);
        System.out.println("请求密文：" + requestString);
        //请求接口
        String result = HttpsUtil.post(DSPConsts.TX_URL_TEST, requestString, null);
        System.out.println("响应密文：" + result);

        String respPlainText = null;
        String[] respMsg = result.split(",");
        if (respMsg.length == 1 && -1 == respMsg[0].indexOf(',')) {
            respPlainText = Base64.decode(respMsg[0], "UTF-8");
        } else if (respMsg.length == 5) {
            String responseMessage = respMsg[0];
            String responseSignature = respMsg[1];
            String responseDgtlEnvlp = respMsg[2];
            String respSignSN = respMsg[3];
            String respEncryptSN = respMsg[4];

            Tx2230Response tx2230Response = new Tx2230Response(responseMessage, responseSignature, responseDgtlEnvlp, respSignSN, respEncryptSN);
            respPlainText = tx2230Response.getResponsePlainText();
            if ("2000".equals(tx2230Response.getCode())) {
                String status = tx2230Response.getStatus();
                System.out.println("验证结果" + status);
            }
        }
        System.out.println("响应明文：" + respPlainText);


    }
}
